<?php
namespace app\admin\controller;
use app\model\GpuModel as GpuModelModel;
use think\Db;


class GpuModel extends Base
{
    public function __construct(){
        $model = new GpuModelModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '地域';

        if ( in_array($page_info['action'], ['add','edit']) ) {
            $fields = [
                'name'=>['name'=>'名称','type'=>'text'],
                'sort'=>['name'=>'排序值','type'=>'text'],
            ];
            $valids = createPageValid($fields);
            $this->assign([
                'fields'=>$fields,
                'valids'=>$valids
            ]);
        }

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'name','type'=>'like']
        ];
        $map = getSearchParam($map_params);
      
        $search_params = [
            [
                ['key'=>'name','name'=>'名称','type'=>'text'],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);

        return parent::index($map);
    }


    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['name'],
                'nomust'=>['sort']
            ];
            $save_data = paramFilter(request()->param(),$fields);
        }
        return parent::saveData($save_data);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        if (request()->isPost()) {
            $fields = [
                'must'=>['name'],
                'nomust'=>['sort']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }
        return parent::updateData($id,$save_data);
    }


}
